/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.core;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.core.Controller;

import cn.hg.base.common.BaseConstants;

/**
 * @author Golly
 */
public class JfsaThreadLocal {

	private static ThreadLocal<Controller> controllerTL = new ThreadLocal<>();
	private static ThreadLocal<HttpServletRequest> requestTL = new ThreadLocal<>();

	@SuppressWarnings ("unchecked")
	public static <T> T getAdmin () {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return null;
		}

		return (T) JfsaThreadLocal.getRequest().getSession().getAttribute(BaseConstants.CURRENT_ADMIN);
	}

	@SuppressWarnings ("unchecked")
	public static <T> T getAdminAuthAction () {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return null;
		}

		return (T) JfsaThreadLocal.getRequest().getSession().getAttribute(BaseConstants.ADMIN_AUTH_ACTION);
	}

	@SuppressWarnings ("unchecked")
	public static <T> T getAdminAuthMenu () {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return null;
		}

		return (T) JfsaThreadLocal.getRequest().getSession().getAttribute(BaseConstants.ADMIN_AUTH_MENU);
	}

	public static Controller getController () { return JfsaThreadLocal.controllerTL.get(); }

	public static HttpServletRequest getRequest () { return JfsaThreadLocal.requestTL.get(); }

	@SuppressWarnings ("unchecked")
	public static <T> T getUser () {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return null;
		}

		return (T) JfsaThreadLocal.getRequest().getSession().getAttribute(BaseConstants.CURRENT_USER);
	}

	@SuppressWarnings ("unchecked")
	public static <T> T getUserAuthAction () {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return null;
		}

		return (T) JfsaThreadLocal.getRequest().getSession().getAttribute(BaseConstants.USER_AUTH_ACTION);
	}

	@SuppressWarnings ("unchecked")
	public static <T> T getUserAuthMenu () {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return null;
		}

		return (T) JfsaThreadLocal.getRequest().getSession().getAttribute(BaseConstants.USER_AUTH_MENU);
	}

	public static void setAdmin (Object admin) {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return;
		}

		JfsaThreadLocal.getRequest().getSession().setAttribute(BaseConstants.CURRENT_ADMIN, admin);
	}

	public static void setAdminAuthAction (Object action) {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return;
		}

		JfsaThreadLocal.getRequest().getSession().setAttribute(BaseConstants.ADMIN_AUTH_ACTION, action);
	}

	public static void setAdminAuthMenu (Object menu) {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return;
		}

		JfsaThreadLocal.getRequest().getSession().setAttribute(BaseConstants.ADMIN_AUTH_MENU, menu);
	}

	public static void setController (Controller c) {
		JfsaThreadLocal.controllerTL.set(c);
	}

	public static void setRequest (HttpServletRequest request) {
		JfsaThreadLocal.requestTL.set(request);
	}

	public static void setUser (Object user) {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return;
		}

		JfsaThreadLocal.getRequest().getSession().setAttribute(BaseConstants.CURRENT_USER, user);
	}

	public static void setUserAuthAction (Object action) {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return;
		}

		JfsaThreadLocal.getRequest().getSession().setAttribute(BaseConstants.USER_AUTH_ACTION, action);
	}

	public static void setUserAuthMenu (Object menu) {

		if (null == JfsaThreadLocal.getRequest() || null == JfsaThreadLocal.getRequest().getSession()) {
			return;
		}

		JfsaThreadLocal.getRequest().getSession().setAttribute(BaseConstants.USER_AUTH_MENU, menu);
	}

}
